package _24_二叉树中和为某一值的路径;

import java.util.ArrayList;

public class Solution2 {
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();

        ArrayList<Integer> list = new ArrayList<>();
        find(root,target,res,list);
        return res;
    }

    void find(TreeNode root, int target, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> list) {
        //如果根为空，或者其值大于
        if (root == null ) {
            return;
        }
        target -=root.val;
        // ArrayList<Integer> tmp = new ArrayList<>(list);

        list.add(root.val);
        if (0 == target &&root.left==null &&root.right==null) {

            res.add(new ArrayList<Integer>(list));

        }
        find(root.left,target,res,list);
        find(root.right,target,res,list);
        list.remove(list.size()-1);




    }
}
